/**

This file is part of MaCI/GIMnet.

MaCI/GIMnet is free software: you can redistribute it and/or modify it 
under the terms of the GNU Lesser General Public License as published 
by the Free Software Foundation, either version 3 of the License, or 
(at your option) any later version.

MaCI/GIMnet is distributed in the hope that it will be useful, but WITHOUT 
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public 
License for more details.

You should have received a copy of the GNU Lesser General Public 
License along with GIMnet. (See COPYING.LESSER) If not, see 
<http://www.gnu.org/licenses/>.

**/
/** 
 * \file
 * \brief Motti Task Generator
 * \author Antti Maula <antti.maula@tkk.fi>
 *
 */

#include "MottiMission.hpp"
#include "owndebug.h"
#include <assert.h>
#include <stdio.h>
#include <vector>

//*****************************************************************************
int PICK_COUNT = 5;
int FILL_PICK_MissionLocation_COUNT = 1;
int TRANSPORT_MissionLocation_COUNT = 2;
int PALLET_COUNT = 3;
//*****************************************************************************
//*****************************************************************************
typedef std::pair<std::string, unsigned int> TProductAndId;
typedef std::vector<TProductAndId> TProductAndIdArray;
//*****************************************************************************
//*****************************************************************************
//*****************************************************************************
TProductAndIdArray products;
//*****************************************************************************
//*****************************************************************************
//*****************************************************************************

void InitProducts(void)
{
  int id = 0;
  products.clear();
  products.push_back( TProductAndId( "Banana", ++id) );
  products.push_back( TProductAndId( "Beer", ++id) );
  products.push_back( TProductAndId( "Monkeys", ++id) );
  products.push_back( TProductAndId( "Pizza", ++id) );
  products.push_back( TProductAndId( "Computers", ++id) );
  products.push_back( TProductAndId( "Capacitors", ++id) );
  products.push_back( TProductAndId( "Salmiakki", ++id) );
  products.push_back( TProductAndId( "Rocks", ++id) );
  products.push_back( TProductAndId( "Staples", ++id) );
  products.push_back( TProductAndId( "DVDs", ++id) );
  products.push_back( TProductAndId( "Bits", ++id) );
  products.push_back( TProductAndId( "Nuts'n'Bolts", ++id) );
  products.push_back( TProductAndId( "Code", ++id) );
  products.push_back( TProductAndId( "GIMnet", ++id) );
  products.push_back( TProductAndId( "Mobile Phones", ++id) );
  products.push_back( TProductAndId( "TVs", ++id) );
  products.push_back( TProductAndId( "Microwave Ovens", ++id) );
  products.push_back( TProductAndId( "Paper", ++id) );
  products.push_back( TProductAndId( "Frisbees", ++id) );
  products.push_back( TProductAndId( "Ducks", ++id) );
  products.push_back( TProductAndId( "Chickens", ++id) );
  products.push_back( TProductAndId( "Letters", ++id) );
  products.push_back( TProductAndId( "Numbers", ++id) );
  products.push_back( TProductAndId( "Cement", ++id) );
}
//*****************************************************************************

TProductAndId GetProduct()
{
  assert(products.size());
  return products[ (int)ownRandom_get_range( 0, products.size() ) ];
}
//*****************************************************************************

Motti::SNodeHeader GetEmptyNodeHeader(void)
{
  return Motti::SNodeHeader("","AND","NONE");
} 
//*****************************************************************************

Motti::SNodeHeader GetNodeHeader(const std::string &aRule = "AND", 
                                 const std::string &aTrigger = "NONE") 
{
  int 
    a = (int)ownRandom_get_range(0, 100),
    b = (int)ownRandom_get_range(0, 100),
    c = (int)ownRandom_get_range(0, 100),
    d = (int)ownRandom_get_range(0, 100),
    e = (int)ownRandom_get_range(0, 100);

  char id[128];
  sprintf(id,"%u.%u.%u.%u.%u",a,b,c,d,e);

  return Motti::SNodeHeader(id,aRule,aTrigger);
}
//*****************************************************************************

int main(int argc, char *argv[])
{
  using namespace Motti;
  bool r;
  std::string targetfile = "generated-task.xml";

  // If two params, (or more); use 2nd as targetfile.
  if (argc > 1) {
    targetfile = argv[1];
  }

  // Normal debug lib inits.
  debugInit();
  debugSetGlobalDebugLvl(5);

  // Init
  InitProducts();

  // Foo.
  SMissionHeader missionHeader("Nimi", 
                               "kuvaus", 
                               gim::time(true), 
                               SMissionLocation(GetNodeHeader(), 
                                         ownRandom_get_range(-10,10), 
                                         ownRandom_get_range(-10,10)), 
                               (int)ownRandom_get_range(0,1000));
  SPallets pallets;
  for(int p=0; p < PALLET_COUNT; ++p) {
    SGetEmpty ge(GetEmptyNodeHeader(), 
                 SMissionLocation(GetEmptyNodeHeader(), 
                           ownRandom_get_range(-10,10), 
                           ownRandom_get_range(-10,10)));

    SFill f(GetNodeHeader());
    for(int fi=0; fi < PICK_COUNT; ++fi) {
      TMissionLocationArray MissionLocations;
      for(int l=0; l < FILL_PICK_MissionLocation_COUNT; ++l) {
        SMissionLocation nl(GetNodeHeader(),
                     ownRandom_get_range(-10,10),
                     ownRandom_get_range(-10,10),
                     l);
        MissionLocations.push_back(nl);
      }
      
      TProductAndId paid = GetProduct();
      std::string pick_trigger = "NONE";
      if (f.picks.size()) pick_trigger = f.picks.back().nodeheader.id;

      SPick fp(GetNodeHeader("AND", pick_trigger),
               paid.first,
               paid.second,
               "10kg",
               MissionLocations);
      
      f.AddPick(fp);
    }
    
    TMissionLocationArray transportMissionLocations;
    for(int tl=0; tl < TRANSPORT_MissionLocation_COUNT; ++tl) {
      SMissionLocation trl(GetNodeHeader("OR"),
                    ownRandom_get_range(-10,10),
                    ownRandom_get_range(-10,10),
                    tl);
      transportMissionLocations.push_back(trl);
    }
    
    std::string transport_trigger = "NONE";
    if (f.picks.size()) transport_trigger = f.picks.back().nodeheader.id;

    STransport t(GetNodeHeader("AND", transport_trigger),
                 transportMissionLocations);
    
    SPallet np(GetNodeHeader(), ge, f, t);
    pallets.AddPallet(np);
  }


  SMission m(GetNodeHeader(), missionHeader, pallets);

  CMission mission;
  r = mission.EncodeMottiMission(m);
  assert(r);

  // ... and if targetfilename is set; write the same information to
  // the file as well.
  if (targetfile.size()) {
    FILE *f = fopen(targetfile.c_str(), "w");
    if (f) {
      fprintf(f, "%s\n", m.ToXMLString().c_str());
      fclose(f);
    }

  } else {
    fprintf(stdout, "%s\n", m.ToXMLString().c_str());

  }

  return 0;
}
//*****************************************************************************
//*****************************************************************************
